package com.xxyuan.batch._06_context;

import com.xxyuan.batch._05_job_listener.JobStateAnnoListener;
import com.xxyuan.batch._05_job_listener.JobStateListener;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.listener.JobListenerFactoryBean;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.Map;

@SpringBootApplication
@EnableBatchProcessing
public class ExecutionContextJob {
    /**
     * job调度器
     */
    @Autowired
    private JobLauncher jobLauncher;

    /***
     * job构造器工厂
     */
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    /**
     * step构造器工厂
     */
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    /**
     * 构造一个step对象执行的任务(逻辑对象)
     */
    @Bean
    public Tasklet tasklet1() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                //步骤
                //可以共享数据,但是不允许修改
                //Map<String, Object> stepExecutionContext = chunkContext.getStepContext().getStepExecutionContext();
                ExecutionContext stepEx = chunkContext.getStepContext().getStepExecution().getExecutionContext();
                stepEx.put("key-step1-step","value-step1-step");
                System.out.println("-----------------1-------------------");
                //作业
                ExecutionContext jobEx = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext();
                jobEx.put("key-step1-job","value-step1-job");


                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Tasklet tasklet2() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                //步骤
                ExecutionContext stepEx = chunkContext.getStepContext().getStepExecution().getExecutionContext();
                System.err.println(stepEx.get("key-step1-step"));
                System.out.println("-----------------2-------------------");
                //作业
                ExecutionContext jobEx = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext();
                System.err.println(jobEx.get("key-step1-job"));
                return RepeatStatus.FINISHED;

            }
        };
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1").tasklet(tasklet1())
                .build();
    }

    @Bean
    public Step step2() {
        return stepBuilderFactory.get("step2").tasklet(tasklet2())
                .build();
    }

    @Bean
    public Job job() {
        return jobBuilderFactory.get("api-execution-context-job")
                .start(step1())
                .next(step2())
                .incrementer(new RunIdIncrementer())
                .build();
    }

    public static void main(String[] args) {
        SpringApplication.run(ExecutionContextJob.class, args);
    }
}